//
//  ForgetViewController.m
//  digitalCurrency
//
//  Created by sunliang on 2018/1/30.
//  Copyright © 2018年 ZhengTuo. All rights reserved.
//

#import "ForgetViewController.h"

@interface ForgetViewController ()
{
    NSInteger _countDown;
    NSTimer *_timer;
}

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTF;
@property (weak, nonatomic) IBOutlet UITextField *verifyCodeTF;
@property (weak, nonatomic) IBOutlet UITextField *passWordTF;
@property (weak, nonatomic) IBOutlet UITextField *confirmTF;
@property (weak, nonatomic) IBOutlet UIButton *verifyButton;

@property (weak, nonatomic) IBOutlet UIButton *okBtn;

@property (weak, nonatomic) IBOutlet UILabel *phonelabel;

@property (weak, nonatomic) IBOutlet UILabel *verifylabel;
@property (weak, nonatomic) IBOutlet UILabel *newpasswordlabel;
@property (weak, nonatomic) IBOutlet UILabel *confirflabel;
@property (weak, nonatomic) IBOutlet UIButton *eyebutton1;
@property (weak, nonatomic) IBOutlet UIButton *eyebutton2;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verifyLabelTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verifyLabelHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verifyTFTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verifyTFHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verifyBtnConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewTopConstraint;
@property (weak, nonatomic) IBOutlet UIView *lineView;


@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _countDown = 60;
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"忘记密码";
    [_phoneNumberTF setValue:RGBOF(0x83868C) forKeyPath:@"_placeholderLabel.textColor"];
    [_verifyCodeTF setValue:RGBOF(0x83868C) forKeyPath:@"_placeholderLabel.textColor"];
    [_passWordTF setValue:RGBOF(0x83868C) forKeyPath:@"_placeholderLabel.textColor"];
    [_confirmTF setValue:RGBOF(0x83868C) forKeyPath:@"_placeholderLabel.textColor"];
//
    [_phoneNumberTF setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    [_verifyCodeTF setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    [_passWordTF setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    [_confirmTF setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];


    [self.verifyButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    [self.okBtn setTitle:@"保存" forState:UIControlStateNormal];
    self.phoneNumberTF.placeholder = @"请输入手机号";
    self.verifyCodeTF.placeholder = @"请输入手机验证码";
    self.passWordTF.placeholder = @"请输入新密码";
    self.confirmTF.placeholder = @"请再次输入新密码";
    
    self.phonelabel.text = @"手机号";
    self.verifylabel.text = @"验证码";
    self.newpasswordlabel.text = @"新密码";
    self.confirflabel.text = @"确认密码";

    self.okBtn.layer.masksToBounds = YES;
    self.okBtn.layer.cornerRadius = kWindowW * 0.7 * 46 / 256 / 2;
}

- (IBAction)verifyAction:(UIButton *)sender {
    [self.view endEditing:YES];
    //先判断手机号
    if (self.phoneNumberTF.text.length <= 0) {
        [self.view makeToast:@"请输入您的手机号" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    self.verifyButton.enabled = NO;
    
}

- (void)getVerifyWithticket:(NSString *)ticket randstr:(NSString *)randstr{
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"uc/sms/verify_codes"];
    NSDictionary *param = @{@"userMobile":self.phoneNumberTF.text, @"ticket":ticket, @"randStr":randstr};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"验证码 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
            self.verifyButton.enabled = YES;
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self-> _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
            }else{
                self.verifyButton.enabled = YES;
                [self.view makeToast:responseResult[@"returnMessage"] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

#pragma mark - PrivateMethod
- (void)onTimer{
    if (_countDown > 0) {
        [self.verifyButton setTitle:[NSString stringWithFormat:@"%ld秒重新获取", (long)_countDown] forState:UIControlStateNormal];
        _countDown--;
    }else{
        _countDown = 60;
        [_timer invalidate];
        _timer = nil;
        [self.verifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.verifyButton.enabled = YES;
    }
}

- (IBAction)openTf1:(id)sender {
    self.eyebutton1.selected = !self.eyebutton1.selected;
    self.passWordTF.secureTextEntry = !self.eyebutton1.selected;
}
- (IBAction)openTF2:(id)sender {
    self.eyebutton2.selected = !self.eyebutton2.selected;
    self.confirmTF.secureTextEntry = !self.eyebutton2.selected;
}

#pragma mark-确定
- (IBAction)confirm:(UIButton *)sender {
    if ([NSString stringIsNull:self.phoneNumberTF.text]) {
        [self.view makeToast:@"请输入手机号" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.verifyCodeTF.text]) {
        [self.view makeToast:@"请输入验证码" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.passWordTF.text]) {
        [self.view makeToast:@"请输入新密码" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.confirmTF.text]) {
        [self.view makeToast:@"请再次输入密码" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (![self.passWordTF.text isEqualToString:self.confirmTF.text]) {
        [self.view makeToast:@"您两次输入的密码不一致" duration:1.5 position:CSToastPositionCenter];
        return;
    }

    [EasyShowLodingView showLodingText:Loading];

    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"uc/user/reset_password"];
    NSDictionary *param = @{@"mobilePhone":self.phoneNumberTF.text, @"oldPassword":[self.passWordTF.text aci_encryptWithAES], @"code":self.verifyCodeTF.text, @"newPassword":[self.confirmTF.text aci_encryptWithAES]};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"忘记密码 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[@"returnCode"] integerValue] == 0) {
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"returnMessage"] duration:1.5 position:CSToastPositionCenter];
                [[AppDelegate sharedAppDelegate] popViewController];
            }else{
                [self.view makeToast:responseResult[@"returnMessage"] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
